this.throwMode = 'touch';
var pageCount= $('[data-role=page]').length;
var equipment="PC";
var current_area=window.location.hash ? parseInt(window.location.hash.substr(5)) : 1;
var url="";
var csstools={"webtools":"css/homecss/web.css","webipad":"css/homecss/ipad.css","webipadtools_h":"css/homecss/ipad_tolls_h.css","webipadgames_h":"css/homecss/ipad_games_h.css","webphone":"css/homecss/iphone.css"};
var cssweather={"webtools":"css/weathercss/web_weather.css","webipad":"css/weathercss/ipad_weather.css","webipadtools_h":"css/weathercss/ipad_weather_h.css","webphone":"css/weathercss/iphone_weather.css"};
var cssip={"webtools":"css/ipcss/web_ip.css","webipad":"css/ipcss/ipad_ip.css","webipadtools_h":"css/ipcss/ipad_ip_h.css","webphone":"css/ipcss/iphone_ip.css"};
var cssgamedetail={"webtools":"css/gamedetail/web_gamesContent.css","webipad":"css/gamedetail/ipad_gamesContent.css","webipadtools_h":"css/gamedetail/ipad_gamesContent_h.css","webphone":"css/gamedetail/iphone_gamesContent.css"};
var cssclock={"webtools":"css/clock/web_timer.css","webipad":"css/clock/ipad_timer.css","webipadtools_h":"css/clock/ipad_timer_h.css","webphone":"css/clock/iphone_timer.css"};
var languagelist=["English","Chinese"];
$(function(){
	 
	$("#page_container").bind('swipeleft',function(event, ui){
		current_area = window.location.hash ? parseInt(window.location.hash.substr(5)) : 1;
		if(current_area < $('[data-role=page]').length){
			window.location.hash = '#area' + (current_area + 1);
			if($("#page_bubbles").length>0){
				changebubble();
			}
			if($("#page_outbox").length>0){
				$("#page_box1").html((current_area+1)+"/"+$('[data-role=page]').length);
			}
		}
	})
	$("#page_container").bind('swiperight',function(event, ui){
		current_area = window.location.hash ? parseInt(window.location.hash.substr(5)) : 1;
		if(current_area>1){
			window.location.hash = '#area' + (current_area - 1);
			if($("#page_bubbles").length>0){
				changebubble();
			}
			if($("#page_outbox").length>0){
				$("#page_box1").html((current_area-1)+"/"+$('[data-role=page]').length);
			}
		}
	})
	
})

function changebubble(){
	var current_area=window.location.hash ? parseInt(window.location.hash.substr(5)) : 1;
	var countpages=$('div[data-role="page"]').length;
	var ul=document.getElementById("page_bubbles");
	var rowCount = $('#page_bubbles >li').length;
	if(rowCount<countpages){
		for(var i=0;i<countpages;i++){
			var li=document.createElement("li");
			li.className="bubbles_left";
			li.id="buddleli"+(i+1);
			ul.appendChild(li);
		}
	}
	var li= document.getElementById("buddleli" + current_area);
	li.className="bubbles_hover";
	var current_area=window.location.hash ? parseInt(window.location.hash.substr(5)) : 1;
	if(current_area-1>0){
		li=document.getElementById("buddleli"+(current_area-1));
		li.className="bubbles_left";
	}else if(current_area+1<=countpages){
		li=document.getElementById("buddleli"+(current_area+1));
		li.className="bubbles_left";
	}
}


(function checkSupport() {
    var ua = navigator.userAgent;
    var iosBrowserPattern = /(iPad|iPod).+OS\s\d/;
    var iphoneBrowserPatern=/(iPhone).+OS\s\d/;
    var androidBrowserPattern = /Android\s(\d)/;
    var matchIos = iosBrowserPattern.exec(ua);
    var matchiphone=iphoneBrowserPatern.exec(ua);
    var matchAndroid = androidBrowserPattern.exec(ua);
    if(/(Android)/i.test(ua)){
    	equipment="androdiphone";
    }else if(matchIos && matchIos[1] == 'iPad'){
    	equipment="pad";
    }else if(/(iPhone|iPod)/i.test(ua)){
    	equipment="iphone";
    }
})();



function addMetaFile(cssfile,param){
  var fileref=document.createElement("meta");
  fileref.name = "viewport";
  if ("pad"==equipment) {
    fileref.content="initial-scale=1,maximum-scale=1";
  } else if("androdiphone"==equipment){
    //fileref.content="initial-scale=0.65,width=device-width,target-densitydpi=320dpi";
	  fileref.content="width=640,target-densitydpi=device-dpi";
  }else if("iphone"==equipment){
	//fileref.content="initial-scale=0.5,width=device-width";
	  fileref.content="width=640,target-densitydpi=device-dpi";
  }
  var headobj = document.getElementsByTagName('head')[0];
  headobj.appendChild(fileref);
  if("pad"==equipment){
	  var orient="portrait";
	  addCssFile(cssfile,param,orient);
	  orient="landscape";
	  addCssFile(cssfile,param,orient);
  }else{
	  addCssFile(cssfile);
  }
}

function addCssFile(cssfile,param,orient){
	 if(cssfile=="homecss"){
		 cssfile=csstools;
	 }else if(cssfile=="weathercss"){
		 cssfile=cssweather;
	 }else if(cssfile=="ipcss"){
		 cssfile=cssip
	 }else if(cssfile=="clockcss"){
		 cssfile=cssclock;
	 }else if(cssfile=="gamedetail"){
		 cssfile=cssgamedetail;
	 }
	 var fileref=document.createElement("link");
	  fileref.rel = "stylesheet";
	  if ("pad"==equipment) {
		 if(orient=="landscape"){
			fileref.media="all and (orientation:landscape)";
			if(param=="tools")
				fileref.href=cssfile.webipadtools_h;
			else
				fileref.href=cssfile.webipadgames_h;
			}else{
			fileref.media="all and (orientation:portrait)";
			fileref.href=cssfile.webipad;
		}
	  } else if(equipment.indexOf("phone")!=-1){
		  fileref.href=cssfile.webphone;
	  }else{
		  fileref.href=cssfile.webtools;
	  }
	  console.log(fileref.href+"--"+fileref.media);
	  var headobj = document.getElementsByTagName('head')[0];
	  headobj.appendChild(fileref);
}

window.applicationCache.addEventListener('updateready', function(e) {
  if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
    window.applicationCache.swapCache();
    if (confirm('A new version of this site is available. Load it?')) {
      window.location.reload();
    }
  }
}, false);

function forward(action){ 
	if(window.localStorage.getItem("url")){
		url="?hl="+window.localStorage.getItem("url");
	}
	window.location.href="jsp/"+action+".jsp"+url; 
}

$(function(){
	
    $("#right_games_button").bind('click',function(){
    	if(window.localStorage.getItem("url")){
    		url="?hl="+window.localStorage.getItem("url");
    	}
        window.location.href="html5GameShow.jsp"+url; 
    })
    $("#left_tools_button").bind('click',function(){
    	if(window.localStorage.getItem("url")){
    		url="?hl="+window.localStorage.getItem("url");
    	}
        window.location.href="html5ToolsShow.jsp"+url; 
    })
});

$(function(){
	$("#choice_language").bind('click',function(){
		$("#language_box").show();
    })
    creatlanguageselect();
    var selected=window.localStorage.getItem("hl");
	 if(selected){
		window.localStorage.setItem("url",selected!="Chinese"?"en":"zh-cn");
	}
});


function androidphoneadddiv(){
	if(equipment=="androdiphone"){
		document.getElementById("page_container").className="androdiphone";
	}
	
}

function creatlanguageselect(){
	var div = document.getElementById("language_box");
    var ul = document.createElement("ul");
    
    ul.id="language_list";
    div.appendChild(ul);
    for(var i=0;i<languagelist.length;i++){
    	var li=document.createElement("li");
    	li.id="lan_"+i;
    	li.className="language";
    	li.innerHTML=languagelist[i];
    	ul.appendChild(li);
        (function(id){ li.addEventListener("click",function(e){
    		 changelanguage(id);
    	},false)})(li.id);
    }
}

function changelanguage(e){
	var language=document.getElementById(e).textContent;
	window.localStorage.setItem("hl",language);
	var newhref=document.location.href;
	var hrf=newhref.split("=");
	if(hrf[1]){
		document.location.href=hrf[0]+"="+(language!="Chinese"?"en":"zh-cn");
	}else{
		document.location.href=hrf[0]+"?hl="+(language!="Chinese"?"en":"zh-cn");
	}
}

